/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.lifecycle

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class DefaultLifecycleObserverTest {
    private lateinit var owner: LifecycleOwner
    private lateinit var lifecycle: Lifecycle

    // 初始化测试对象
    fun initTestObjects() {
        // 创建一个简单的 LifecycleOwner 实现
        owner = object : LifecycleOwner {
            override val lifecycle: Lifecycle
                get() = this@DefaultLifecycleObserverTest.lifecycle
        }
        
        // 创建一个实现了 TestLifecycle 接口的 Lifecycle 实现
        lifecycle = object : Lifecycle(), TestLifecycle {
            private var _currentState: State = State.INITIALIZED
            
            override val currentState: State
                get() = _currentState
                
            override fun setCurrentState(state: State) {
                _currentState = state
            }
            
            override fun addObserver(observer: LifecycleObserver) {
                // 简化实现，仅用于测试
            }
            
            override fun removeObserver(observer: LifecycleObserver) {
                // 简化实现，仅用于测试
            }
        }
    }

    @Test
    fun eachEvent() {
        initTestObjects()
        
        // 创建测试用的 DefaultLifecycleObserver 实现
        val testObserver = TestDefaultLifecycleObserver()
        val adapter = DefaultLifecycleObserverAdapter(testObserver, null)
        
        // 测试 ON_CREATE 事件
        (lifecycle as TestLifecycle).setCurrentState(Lifecycle.State.CREATED)
        adapter.onStateChanged(owner, Lifecycle.Event.ON_CREATE)
        assertEquals(1, testObserver.onCreateCallCount)
        
        // 测试 ON_START 事件
        (lifecycle as TestLifecycle).setCurrentState(Lifecycle.State.STARTED)
        adapter.onStateChanged(owner, Lifecycle.Event.ON_START)
        assertEquals(1, testObserver.onStartCallCount)
        
        // 测试 ON_RESUME 事件
        (lifecycle as TestLifecycle).setCurrentState(Lifecycle.State.RESUMED)
        adapter.onStateChanged(owner, Lifecycle.Event.ON_RESUME)
        assertEquals(1, testObserver.onResumeCallCount)
        
        // 测试 ON_PAUSE 事件
        (lifecycle as TestLifecycle).setCurrentState(Lifecycle.State.STARTED)
        adapter.onStateChanged(owner, Lifecycle.Event.ON_PAUSE)
        assertEquals(1, testObserver.onPauseCallCount)
        
        // 测试 ON_STOP 事件
        (lifecycle as TestLifecycle).setCurrentState(Lifecycle.State.CREATED)
        adapter.onStateChanged(owner, Lifecycle.Event.ON_STOP)
        assertEquals(1, testObserver.onStopCallCount)
        
        // 测试 ON_DESTROY 事件
        (lifecycle as TestLifecycle).setCurrentState(Lifecycle.State.INITIALIZED)
        adapter.onStateChanged(owner, Lifecycle.Event.ON_DESTROY)
        assertEquals(1, testObserver.onDestroyCallCount)
        
        // 测试 ON_ANY 事件抛出异常
        assertFailsWith<IllegalArgumentException> {
            adapter.onStateChanged(owner, Lifecycle.Event.ON_ANY)
        }
    }

    @Test
    fun defaultLifecycleObserverAndLifecycleEventObserver() {
        initTestObjects()
        
        val testObserver = TestAllObservers()
        val adapter = DefaultLifecycleObserverAdapter(testObserver, testObserver)
        
        (lifecycle as TestLifecycle).setCurrentState(Lifecycle.State.CREATED)
        adapter.onStateChanged(owner, Lifecycle.Event.ON_CREATE)
        assertEquals(1, testObserver.onCreateCallCount)
        assertEquals(Lifecycle.Event.ON_CREATE, testObserver.lastEvent)
        
        testObserver.reset()
        (lifecycle as TestLifecycle).setCurrentState(Lifecycle.State.STARTED)
        adapter.onStateChanged(owner, Lifecycle.Event.ON_START)
        assertEquals(1, testObserver.onStartCallCount)
        assertEquals(Lifecycle.Event.ON_START, testObserver.lastEvent)
        
    }

    private class TestDefaultLifecycleObserver : DefaultLifecycleObserver {
        var onCreateCallCount = 0
        var onStartCallCount = 0
        var onResumeCallCount = 0
        var onPauseCallCount = 0
        var onStopCallCount = 0
        var onDestroyCallCount = 0
        
        override fun onCreate(owner: LifecycleOwner) {
            onCreateCallCount++
        }
        
        override fun onStart(owner: LifecycleOwner) {
            onStartCallCount++
        }
        
        override fun onResume(owner: LifecycleOwner) {
            onResumeCallCount++
        }
        
        override fun onPause(owner: LifecycleOwner) {
            onPauseCallCount++
        }
        
        override fun onStop(owner: LifecycleOwner) {
            onStopCallCount++
        }
        
        override fun onDestroy(owner: LifecycleOwner) {
            onDestroyCallCount++
        }
    }

    private class TestAllObservers : DefaultLifecycleObserver, LifecycleEventObserver {
        var onCreateCallCount = 0
        var onStartCallCount = 0
        var lastEvent: Lifecycle.Event? = null
        
        fun reset() {
            onCreateCallCount = 0
            onStartCallCount = 0
            lastEvent = null
        }
        
        override fun onCreate(owner: LifecycleOwner) {
            onCreateCallCount++
        }
        
        override fun onStart(owner: LifecycleOwner) {
            onStartCallCount++
        }
        
        override fun onResume(owner: LifecycleOwner) {}
        override fun onPause(owner: LifecycleOwner) {}
        override fun onStop(owner: LifecycleOwner) {}
        override fun onDestroy(owner: LifecycleOwner) {}
        
        override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
            lastEvent = event
        }
    }
    
    private interface TestLifecycle {
        fun setCurrentState(state: Lifecycle.State)
    }
}